﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.Data;
using BALayer;

namespace QuanLy_Truong_THPT
{
    public partial class frmXemHS : Form
    {
        DBHocSinh dbHocSinh;
        DBLop dbLop;
        //doi tuong hien thi len form
        DataTable dtHocSinh = null;
        //doi tuong dua du lieu vao DataTable dtLop
        DataTable dtLop = null;

        DataView dtvHocSinh = null;
        public frmXemHS()
        {
            InitializeComponent();
            dbHocSinh = new DBHocSinh();
            dbLop = new DBLop();
        }
        void LoadData()
        {
            try
            {
                // Vận chuyển dữ liệu vào DataTable dtLop
                dtLop = new DataTable();
                dtLop.Clear();
                dtLop = dbLop.LayLop().Tables[0];
                // Đưa dữ liệu lên ComboBox trong DataGridView  
                (dgvHocSinh.Columns["Lop"] as
                DataGridViewComboBoxColumn).DataSource = dtLop;
                (dgvHocSinh.Columns["Lop"] as
                DataGridViewComboBoxColumn).DisplayMember =
                "TenLop";
                (dgvHocSinh.Columns["Lop"] as
                DataGridViewComboBoxColumn).ValueMember =
                "MaLop";

                //van chuyen Datatable vao dtHocSinh
                dtHocSinh = new DataTable();
                dtHocSinh.Clear();
                dtHocSinh = dbHocSinh.LayHocSinh().Tables[0];
                //dua du lieu len DataGridView
                dgvHocSinh.DataSource = dtHocSinh;

                // Đưa dữ liệu lên ComboBox 
                this.cbLop.DataSource = dtLop;
                this.cbLop.DisplayMember = "TenLop";
                this.cbLop.ValueMember = "MaLop";

                txtTongHS.Visible = false;
                lblSiSo.Visible = false;
            }
            catch (SqlException)
            {
                MessageBox.Show("Không lấy được nội dung trong table Sinh Vien. Lỗi rồi!!!");

            }
        }

        private void btnTroVe_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void frmXemHS_Load(object sender, EventArgs e)
        {
            LoadData();
        }

        private void btnXem_Click(object sender, EventArgs e)
        {
            string strMaLop = cbLop.SelectedValue.ToString();
            dtvHocSinh = new DataView(dtHocSinh);
            dtvHocSinh.RowFilter = "MaLop='" + strMaLop + "'";
            dgvHocSinh.DataSource = dtvHocSinh;

            txtTongHS.Visible = true;
            lblSiSo.Visible = true;
            lblSiSo.Text = "Sĩ Số:";
            txtTongHS.Text = dtvHocSinh.Count.ToString();
        }

        private void btnReLoad_Click(object sender, EventArgs e)
        {
            LoadData();
        }
    }
}
